package com.ourteam.product.model.dao.impl;

import com.ourteam.product.model.dao.IProductModelPackageDAO;
import com.ourteam.product.model.dao.ProductModelPackage;
import com.ourteam.product.model.dao.ProductModelPackageDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 IRKT 2017
 * Auto Generate Date : 2017-11-17 11:01:12
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractProductModelPackageDAOJdbcImpl extends AbstractSpringDAO
    implements IProductModelPackageDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, DomainId, PackageName, Status, Remarks, PackageType, ModelXML,
            LastModifyTime, LastModifierId, LastModifierName, SubmitterId,
            CreatorName, CreateTime, DomainName, DomainCode, DomainStatus
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.DomainId,
            _DataObjectProperty.PackageName, _DataObjectProperty.Status,
            _DataObjectProperty.Remarks, _DataObjectProperty.PackageType,
            _DataObjectProperty.ModelXML, _DataObjectProperty.LastModifyTime,
            _DataObjectProperty.LastModifierId,
            _DataObjectProperty.LastModifierName,
            _DataObjectProperty.SubmitterId, _DataObjectProperty.CreatorName,
            _DataObjectProperty.CreateTime, _DataObjectProperty.DomainName,
            _DataObjectProperty.DomainCode, _DataObjectProperty.DomainStatus
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] { PackageName };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<ProductModelPackage, ProductModelPackageDAOQueryBean> daoHelper;
    private com.ourteam.product.model.dao.impl.AbstractProductModelObjectDAOJdbcImpl ProductModelObjectDao;

    public AbstractProductModelPackageDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<ProductModelPackage, ProductModelPackageDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.product.model.dao.IProductModelDomainDAO.EntityName)
                                .append(" PRODUCT_MODEL_DOMAIN ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.product.model.dao.IProductModelDomainDAO.Id,
                    com.ourteam.product.model.dao.IProductModelDomainDAO.EntityName +
                    ".", "PRODUCT_MODEL_DOMAIN."));
            relatedJoinSQLBuffer.append(" = ").append(DomainId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aProductModelPackage
         * @throws Exception
         */
    public boolean checkIsUnique(ProductModelPackage aProductModelPackage)
        throws Exception {
        if (aProductModelPackage == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aProductModelPackage.getId());

        valueList.add(aProductModelPackage.getPackageName());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aProductModelPackage
     * @throws Exception
     */
    public void insert(ProductModelPackage aProductModelPackage)
        throws Exception {
        super.doInsert(aProductModelPackage);
    }

    /**
         * Batch Insert Datas
         * @param aProductModelPackage
         * @throws Exception
         */
    public void batchInsert(ProductModelPackage[] aProductModelPackages)
        throws Exception {
        super.doBatchInsert(aProductModelPackages);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(ProductModelPackageDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(ProductModelPackage aProductModelPackage)
        throws Exception {
        delete(aProductModelPackage.getId());
    }

    /**
     * Update Data Selective
     * @param aProductModelPackage
     * @throws Exception
     */
    public void update(ProductModelPackage aProductModelPackage)
        throws Exception {
        if (aProductModelPackage.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aProductModelPackage);

        ProductModelPackage tempObj = findById(aProductModelPackage.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aProductModelPackage, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aProductModelPackages[]
     * @throws Exception
     */
    public void batchUpdate(ProductModelPackage[] aProductModelPackages)
        throws Exception {
        super.doBatchUpdate(aProductModelPackages);
    }

    /**
         * Update Data By Condictions
         * @param aProductModelPackage
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(ProductModelPackage aProductModelPackage,
        ProductModelPackageDAOQueryBean aQueryBean) throws Exception {
        super.doUpdateByQuery(aProductModelPackage, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(ProductModelPackageDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(ProductModelPackage aProductModelPackage)
        throws Exception {
        if (aProductModelPackage.getModelXML() == null) {
            throw new Exception(
                "The ProductModelPackage.modelXML value is not allow NULL");
        }

        if (aProductModelPackage.getLastModifyTime() == null) {
            throw new Exception(
                "The ProductModelPackage.lastModifyTime value is not allow NULL");
        }

        if (aProductModelPackage.getLastModifierName() == null) {
            throw new Exception(
                "The ProductModelPackage.lastModifierName value is not allow NULL");
        }

        if (aProductModelPackage.getPackageType() == null) {
            throw new Exception(
                "The ProductModelPackage.packageType value is not allow NULL");
        }

        if (aProductModelPackage.getPackageName() == null) {
            throw new Exception(
                "The ProductModelPackage.packageName value is not allow NULL");
        }

        if (aProductModelPackage.getStatus() == null) {
            throw new Exception(
                "The ProductModelPackage.status value is not allow NULL");
        }

        if (aProductModelPackage.getRemarks() == null) {
            throw new Exception(
                "The ProductModelPackage.remarks value is not allow NULL");
        }

        if (aProductModelPackage.getDomainName() == null) {
            throw new Exception(
                "The ProductModelPackage.domainName value is not allow NULL");
        }

        if (aProductModelPackage.getDomainCode() == null) {
            throw new Exception(
                "The ProductModelPackage.domainCode value is not allow NULL");
        }

        if (aProductModelPackage.getDomainStatus() == null) {
            throw new Exception(
                "The ProductModelPackage.domainStatus value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public ProductModelPackage findById(final long id)
        throws Exception {
        ProductModelPackageDAOQueryBean queryBean = new ProductModelPackageDAOQueryBean();

        ProductModelPackageDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        ProductModelPackage[] ProductModelPackages = query(queryBean);

        if (ProductModelPackages.length == 0) {
            return null;
        } else {
            return ProductModelPackages[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return ProductModelPackage[]
         * @throws Exception
         */
    public ProductModelPackage[] query(
        ProductModelPackageDAOQueryBean queryBean) throws Exception {
        List result = super.doQuery(queryBean,
                new JdbcProductModelPackageRowMapper());

        return (ProductModelPackage[]) result.toArray(new ProductModelPackage[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        ProductModelPackageDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(ProductModelPackageDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(ProductModelPackage[] aProductModelPackages,
        ProductModelPackageDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aProductModelPackages, queryBean);
    }

    public void synchronize(ProductModelPackage[] aProductModelPackages,
        ProductModelPackageDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<ProductModelPackage> callBack)
        throws Exception {
        daoHelper.synchronize(aProductModelPackages, queryBean, callBack);
    }

    public void setProductModelObjectDao(
        com.ourteam.product.model.dao.IProductModelObjectDAO ProductModelObjectDao) {
        this.ProductModelObjectDao = (com.ourteam.product.model.dao.impl.AbstractProductModelObjectDAOJdbcImpl) ProductModelObjectDao;
    }

    /**
     * Query Related ProductModelObject
    * @param queryBean
     * @throws Exception
     */
    public com.ourteam.product.model.dao.ProductModelObject[] queryProductModelObject(
        ProductModelPackageDAOQueryBean queryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.product.model.dao.IProductModelObjectDAO.PackageId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.product.model.dao.impl.ProductModelObjectDAO.EntityName +
            " ref_ProductModelObject";

        String[] entityFields = com.ourteam.product.model.dao.impl.ProductModelObjectDAO.ENTITY_FIELDS;

        String[] entityProperties = com.ourteam.product.model.dao.impl.ProductModelObjectDAO.ENTITY_PROPERTIES;

        List list = ProductModelObjectDao.doQuery(entityName, entityFields,
                entityProperties, queryBean, joinSQL.toString(),
                new com.ourteam.product.model.dao.impl.ProductModelObjectDAO.JdbcProductModelObjectRowMapper());

        return (com.ourteam.product.model.dao.ProductModelObject[]) list.toArray(new com.ourteam.product.model.dao.ProductModelObject[list.size()]);
    }

    /**
     * Query Related ProductModelObject Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] queryProductModelObjectSelective(
        ProductModelPackageDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addSelectProperty(com.ourteam.product.model.dao.IProductModelObjectDAO.Id,
                "id");

            queryBean.addSelectProperty(com.ourteam.product.model.dao.IProductModelObjectDAO.PackageId,
                "packageId");

            queryBean.addSelectProperty(com.ourteam.product.model.dao.IProductModelObjectDAO.ObjectCode,
                "objectCode");

            queryBean.addSelectProperty(com.ourteam.product.model.dao.IProductModelObjectDAO.ObjectName,
                "objectName");

            queryBean.addSelectProperty(com.ourteam.product.model.dao.IProductModelObjectDAO.ObjectType,
                "objectType");

            queryBean.addSelectProperty(com.ourteam.product.model.dao.IProductModelObjectDAO.IsGenerate,
                "isGenerate");

            queryBean.addSelectProperty(com.ourteam.product.model.dao.IProductModelObjectDAO.Status,
                "status");

            queryBean.addSelectProperty(com.ourteam.product.model.dao.IProductModelObjectDAO.Remarks,
                "remarks");

            queryBean.addSelectProperty(com.ourteam.product.model.dao.IProductModelObjectDAO.SubmitterId,
                "submitterId");

            queryBean.addSelectProperty(com.ourteam.product.model.dao.IProductModelObjectDAO.CreatorName,
                "creatorName");

            queryBean.addSelectProperty(com.ourteam.product.model.dao.IProductModelObjectDAO.CreateTime,
                "createTime");

            queryBean.addSelectProperty(com.ourteam.product.model.dao.IProductModelObjectDAO.LastModifierName,
                "lastModifierName");

            queryBean.addSelectProperty(com.ourteam.product.model.dao.IProductModelObjectDAO.LastModifyTime,
                "lastModifyTime");
        }

        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.product.model.dao.IProductModelObjectDAO.PackageId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.product.model.dao.impl.ProductModelObjectDAO.EntityName +
            " ref_ProductModelObject";

        return ProductModelObjectDao.doQuerySelective(entityName, queryBean,
            joinSQL.toString());
    }

    /**
     * Update Related ProductModelObject
     * @param aProductModelObject
     * @param aQueryBean
     * @model
     * @throws Exception
     */
    public void updateProductModelObject(
        com.ourteam.product.model.dao.ProductModelObject aProductModelObject,
        ProductModelPackageDAOQueryBean aQueryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.product.model.dao.IProductModelObjectDAO.PackageId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.product.model.dao.impl.ProductModelObjectDAO.EntityName +
            " ref_ProductModelObject";

        String[] entityFields = com.ourteam.product.model.dao.impl.ProductModelObjectDAO.ENTITY_FIELDS;

        String[] entityProperties = com.ourteam.product.model.dao.impl.ProductModelObjectDAO.ENTITY_PROPERTIES;

        ProductModelObjectDao.doUpdateByQuery(entityName, entityFields,
            entityProperties, joinSQL.toString(), aProductModelObject,
            aQueryBean);
    }

    /**
     * Delete Related ProductModelObject
     * @param aQueryBean
     * @model
     * @throws Exception
     */
    public void deleteProductModelObject(
        ProductModelPackageDAOQueryBean aQueryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.product.model.dao.IProductModelObjectDAO.PackageId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.product.model.dao.impl.ProductModelObjectDAO.EntityName; // + " ref_ProductModelObject";

        ProductModelObjectDao.doDeleteByQuery(entityName, joinSQL.toString(),
            aQueryBean);
    }

    public static class JdbcProductModelPackageRowMapper implements RowMapper<ProductModelPackage> {
        public ProductModelPackage mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            ProductModelPackage object = new ProductModelPackage();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object domainId = null;

                domainId = resultSet.getObject(columnIndex++);

                object.setDomainIdNull(resultSet.getBoolean(columnIndex++));

                if (domainId != null) {
                    setBeanValue(object, _DataObjectProperty.DomainId, domainId);
                }

                Object packageName = null;

                packageName = resultSet.getObject(columnIndex++);

                object.setPackageNameNull(resultSet.getBoolean(columnIndex++));

                if (packageName != null) {
                    setBeanValue(object, _DataObjectProperty.PackageName,
                        packageName);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object packageType = null;

                packageType = resultSet.getObject(columnIndex++);

                object.setPackageTypeNull(resultSet.getBoolean(columnIndex++));

                if (packageType != null) {
                    setBeanValue(object, _DataObjectProperty.PackageType,
                        packageType);
                }

                Object modelXML = null;

                modelXML = resultSet.getObject(columnIndex++);

                object.setModelXMLNull(resultSet.getBoolean(columnIndex++));

                if (modelXML != null) {
                    setBeanValue(object, _DataObjectProperty.ModelXML, modelXML);
                }

                Object lastModifyTime = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    lastModifyTime = resultSet.getTimestamp(columnIndex++);
                } else {
                    lastModifyTime = resultSet.getObject(columnIndex++);
                }

                object.setLastModifyTimeNull(resultSet.getBoolean(columnIndex++));

                if (lastModifyTime != null) {
                    setBeanValue(object, _DataObjectProperty.LastModifyTime,
                        lastModifyTime);
                }

                Object lastModifierId = null;

                lastModifierId = resultSet.getObject(columnIndex++);

                object.setLastModifierIdNull(resultSet.getBoolean(columnIndex++));

                if (lastModifierId != null) {
                    setBeanValue(object, _DataObjectProperty.LastModifierId,
                        lastModifierId);
                }

                Object lastModifierName = null;

                lastModifierName = resultSet.getObject(columnIndex++);

                object.setLastModifierNameNull(resultSet.getBoolean(
                        columnIndex++));

                if (lastModifierName != null) {
                    setBeanValue(object, _DataObjectProperty.LastModifierName,
                        lastModifierName);
                }

                Object submitterId = null;

                submitterId = resultSet.getObject(columnIndex++);

                object.setSubmitterIdNull(resultSet.getBoolean(columnIndex++));

                if (submitterId != null) {
                    setBeanValue(object, _DataObjectProperty.SubmitterId,
                        submitterId);
                }

                Object creatorName = null;

                creatorName = resultSet.getObject(columnIndex++);

                object.setCreatorNameNull(resultSet.getBoolean(columnIndex++));

                if (creatorName != null) {
                    setBeanValue(object, _DataObjectProperty.CreatorName,
                        creatorName);
                }

                Object createTime = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    createTime = resultSet.getTimestamp(columnIndex++);
                } else {
                    createTime = resultSet.getObject(columnIndex++);
                }

                object.setCreateTimeNull(resultSet.getBoolean(columnIndex++));

                if (createTime != null) {
                    setBeanValue(object, _DataObjectProperty.CreateTime,
                        createTime);
                }

                Object domainName = null;

                domainName = resultSet.getObject(columnIndex++);

                object.setDomainNameNull(resultSet.getBoolean(columnIndex++));

                if (domainName != null) {
                    setBeanValue(object, _DataObjectProperty.DomainName,
                        domainName);
                }

                Object domainCode = null;

                domainCode = resultSet.getObject(columnIndex++);

                object.setDomainCodeNull(resultSet.getBoolean(columnIndex++));

                if (domainCode != null) {
                    setBeanValue(object, _DataObjectProperty.DomainCode,
                        domainCode);
                }

                Object domainStatus = null;

                domainStatus = resultSet.getObject(columnIndex++);

                object.setDomainStatusNull(resultSet.getBoolean(columnIndex++));

                if (domainStatus != null) {
                    setBeanValue(object, _DataObjectProperty.DomainStatus,
                        domainStatus);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
